// 加载express模块
const express = require('express')
const router = express.Router()
const pool = require('../mysql/pool')
const Joi = require('joi')
router.use(express.urlencoded())
router.use(express.json())

/**
 * 查询所有文章
 * @param:
 *  type_id: 1 文章类型
 *  page:1 当前页码
 *  pagesize:10 每页条目数
 * @return:
 *  {status:200,msg:'ok',data:[{文章1},{文章2},{文章3}]
 */

router.get('/',(req,res,next)=>{
    console.log('获取列表请求进来了');
    // 获取请求参数 get请求的参数封装在req.query中
    let {
         type_id,
         page,
         pagesize 
    } = req.query;
    let schema = Joi.object({
        type_id:Joi.number().required().max(10),
        page: Joi.number().required(), 
        pagesize: Joi.number().required().max(30),
    })
    const { error,value } = schema.validate(req.query)
    if(error){
        res.send({
            status:400,
            message:error.message
        })
    }

    // 执行查询业务
    pool.query(`SELECT article.id, article.small_title, article.title, article.content, article.type_id, article.uid, user.name AS username, user.nickname
    FROM article
    JOIN user ON article.uid = user.id
    WHERE article.type_id = ? LIMIT ?,?;`,[+type_id,+page,+pagesize],(err,r)=>{
        if(err){
            return next(err)
        }
        console.log(r);
        res.send({status:'200',message:'ok',data:r})
    })
})

/**
 * 通过文章标题模糊查询文章
 * @param:
 *  title: xxx 文章标题
 *  page:1 当前页码
 *  pagesize:10 每页条目数
 * @return
 *  {status:200,mesg:'ok',data:[{文章1},{文章2},{文章3}]}
 */
router.post('/title',(req,res,next)=>{
    let {
        title,
        page,
        pagesize 
    } = req.body // 获取参数

    // 表单验证
    const schema = Joi.object({
        title:Joi.string().required(),
        page:Joi.number().required(),
        pagesize:Joi.number().required().max(100).min(1)
    })

    // 执行模糊查询业务
    pool.query(`SELECT article.id, article.small_title, article.title, article.content, article.type_id, article.uid, user.name AS username, user.nickname
    FROM article
    JOIN user ON article.uid = user.id article.title like '%?%' LIMIT ?,?;`,[title,page,pagesize],(err,r)=>{
        if(err){
            return next(err)
        }

        res.send({
            status:200,
            message:'ok',
            data:r
        })
    })
})

/**
 * 删除文章业务
 * @param:
 *  id : 文章id
 * @return:
 *  {status:200,message:'ok'}
 */
router.post('/del',(req,res,next)=>{
    const { id } = req.body
    console.log(req.query);
    // 表单验证
    console.log(req.body);
    let schema = Joi.object({
        id: Joi.string().required() //必填
    })
    let {error,value} = schema.validate(req.body);
    console.log(error);
    if(error){
        res.send({
            status:400,
            message:error.message
        })
        return; // 结束
    }
    // 执行删除业务
    pool.query('delete from article where id = ?',[id],(err,r)=>{
        if(err){
            return next(err)
        }
        console.log(r);
        res.send({
            status:"200",
            message:"ok",
        })
    })
})
/**
 * 添加文章业务
 * @param:
 *  详见接口文档
 * @return:
 *  {status:200,message:'ok'}
 */
router.post('/add',(req,res)=>{
    const {
        title,
        content,
        image,
        type_id,
        uid
    } = req.body // post请求参数在req.body中

    // 表单验证
    let schema = Joi.object({
        title: Joi.string().required(), // 字符串类型,必填
        content:  Joi.string().required(), // 字符串类型,必填
        image:  Joi.string().required(), // 字符串类型,必填
        type_id: Joi.number().required(), // 数字类型,必填
        uid:  Joi.number().required(), // 数字类型,必填
    })
    let { error,value } = schema.validate(req.body)
    if(error){
        res.send({
            status:"400",
            message:error.message
        })
    }

    // 执行添加业务
    pool.query('insert into article (title,content,image,type_id,uid) values (?,?,?,?,?)',[title,content,image,+type_id,+uid],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({
            status:200,
            message:'ok'
        })
    })
})

/**
 * 
 * 修改文章信息业务
 * @param:
 *  详见接口文档
 * @return:
 *  {status:200,message:'ok'}
 */
router.post('/update',(req,res)=>{
    const {
        id,
        title,
        content,
        image,
        type_id
    } = req.body 
    // 表单验证
    let schema = Joi.object({
        id: Joi.number().required(),
        title: Joi.string().required(),
        content:  Joi.string().required(), 
        image:  Joi.string().required(), 
        type_id: Joi.number().required(),
    })
    let { error,value } = schema.validate(req.body)
    if(error){
        res.send({
            status:400,
            message:error.message
        })
    }

    // 执行修改业务
    res.send({
        status:200,
        message:'sadasd'
    })
    pool.query('update article set title=?,content=?,image=?,type_id=? where id =?',[title,content,image,type_id,id],(err,r)=>{
        if(err) {
            return next(err)
        }
        res.render({
            status:200,
            message:'ok'
        })
    })
})
 
module.exports = router